package xyz.riceball.springcloudstreambinderredis.component;

import org.springframework.cloud.stream.binder.ExtendedConsumerProperties;
import org.springframework.cloud.stream.binder.ExtendedProducerProperties;
import org.springframework.cloud.stream.provisioning.ConsumerDestination;
import org.springframework.cloud.stream.provisioning.ProducerDestination;
import org.springframework.cloud.stream.provisioning.ProvisioningException;
import org.springframework.cloud.stream.provisioning.ProvisioningProvider;
import xyz.riceball.springcloudstreambinderredis.property.RedisConsumerProperties;
import xyz.riceball.springcloudstreambinderredis.property.RedisProducerProperties;

/**
 * <p>
 * RedisProvisioningProvider
 * </p>
 *
 * @author xiaovcloud
 * @since 2024/6/24 15:54
 */
public class RedisProvisioningProvider implements ProvisioningProvider<
	ExtendedConsumerProperties<RedisConsumerProperties>, ExtendedProducerProperties<RedisProducerProperties>> {

	@Override
	public ProducerDestination provisionProducerDestination(String name, ExtendedProducerProperties<RedisProducerProperties> properties) throws ProvisioningException {
		return new RedisMessageDestination(name);
	}

	@Override
	public ConsumerDestination provisionConsumerDestination(String name, String group, ExtendedConsumerProperties<RedisConsumerProperties> properties) throws ProvisioningException {
		return new RedisMessageDestination(name);
	}

	private static class RedisMessageDestination implements ProducerDestination, ConsumerDestination {
		private final String destination;

		public RedisMessageDestination(final String destination) {
			this.destination = destination;
		}

		@Override
		public String getNameForPartition(int partition) {
			return this.destination;
		}

		@Override
		public String getName() {
			return this.destination;
		}
	}
}
